﻿using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();

            InitData();
        }

        static CookieCollection tmpCookies;

        private void InitData()
        {
        }

        private void GetImage()
        {
            //http://ptlogin2.qq.com/getimage?',Math.random(),'
            string host = "http://ptlogin2.qq.com/getimage?',Math.random(),'";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
            request.KeepAlive = true;
            request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
            request.Referer = host;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
            request.CookieContainer = new CookieContainer();

            Stream dataStream = GetStream(request);
            System.Drawing.Image img = null;
            img = System.Drawing.Image.FromStream(dataStream);
            this.pictureBox1.Image = img;
        }

        private void GetFanhui()
        {
            //得到成功返回
            string host2 = "http://qb.qq.com/index_frame.shtml?" + time + "";
            //string host2 = "http://qb.qq.com/index_frame.shtml?PcacheTime=1290589580";

            Uri URI = new Uri(host2);
            HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(host2);
            request2.Method = "get";

            request2.ServicePoint.Expect100Continue = false;
            request2.Timeout = 30000;
            request2.KeepAlive = true;
            request2.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";

            //request2.ContentType = "application/x-www-form-urlencoded";
            request2.Headers.Add("UA-CPU", "x86");
            request2.Headers.Add("Accept-Encoding", "gzip, deflate");
            request2.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;  Embedded Web Browser from: http://bsalsa.com/; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 1.1.4322; .NET CLR 2.0.50727; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            //request2.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            request2.KeepAlive = true;

            request2.CookieContainer = new CookieContainer();

            foreach (System.Net.Cookie cookie in tmpCookies)
            {
                cookie.Domain = URI.Host;
                if (cookie.Value.Length != 0)
                {
                    request2.CookieContainer.Add(cookie);
                }
                else
                {
                    //Console.WriteLine(cookie.ToString());
                }
            }

            HttpWebResponse myHttpWebResponse = (HttpWebResponse)request2.GetResponse();
            Stream receiveStream = myHttpWebResponse.GetResponseStream();
            Encoding encode = System.Text.Encoding.UTF8;
            //StreamReader readStream = new StreamReader(receiveStream, encode);
            StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.GetEncoding("gb2312"));

            string temp = readStream.ReadToEnd();
            this.richTextBox1.Text = temp;
        }

        string time = "";

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                tmpCookies = new CookieCollection();

                GetImage();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
        }

        public static Stream GetStream(WebRequest request)
        {
            MemoryStream dataStream;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //if (Variant.CurrentUser != null)
            //{
            //    AddCookies(response.Cookies);
            //}

            foreach (System.Net.Cookie newcookie in response.Cookies)
            {
                tmpCookies.Add(newcookie);
            }

            try
            {
                dataStream = CopyStream(response.GetResponseStream());
            }
            finally
            {
                request.Abort();
                response.Close();
            }
            return dataStream;
        }

        private static MemoryStream CopyStream(Stream stream)
        {
            MemoryStream result = new MemoryStream();
            byte[] buffer = new byte[0x1000];
            while (true)
            {
                int size = stream.Read(buffer, 0, 0x1000);
                if (size <= 0)
                {
                    break;
                }
                result.Write(buffer, 0, size);
            }
            result.Seek(0L, SeekOrigin.Begin);
            return result;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string name = this.textBox1.Text;
            string pass = this.textBox2.Text;

            string yanzheng = this.textBox3.Text;

            System.Type type = System.Type.GetTypeFromProgID("MSScriptControl.ScriptControl");
            Object script = System.Activator.CreateInstance(type);
            type.InvokeMember("language", BindingFlags.SetProperty, null, script, new Object[] { "JavaScript" });
            StreamReader sr1 = new StreamReader("ps.js");
            string s = sr1.ReadToEnd();
            type.InvokeMember("AddCode", BindingFlags.InvokeMethod, null, script, new Object[] { s });
            string strtemp = type.InvokeMember("Run", BindingFlags.InvokeMethod, null, script, new Object[] { "hex_hmac_md5", "aR!@$34GP<S#@Js;", pass }).ToString();
            strtemp = yanzheng + strtemp;
            string strtemp1 = type.InvokeMember("Run", BindingFlags.InvokeMethod, null, script, new Object[] { "md5", strtemp }).ToString();
            pass = strtemp1;

            if (yanzheng == "")
            {
                return;
            }

            string host = "http://qb.qq.com/cgi-bin/qbas_login.cgi";
            Uri URI = new Uri(host);
            //string host = "http://www.baidu.com/";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);

            request.ServicePoint.Expect100Continue = false;
            request.Timeout = 30000;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "post";
            request.Headers.Add("UA-CPU", "x86");
            request.Headers.Add("Accept-Encoding", "gzip, deflate");
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;  Embedded Web Browser from: http://bsalsa.com/; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 1.1.4322; .NET CLR 2.0.50727; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            request.KeepAlive = true;
            request.Referer = host;

            request.CookieContainer = new CookieContainer();

            foreach (System.Net.Cookie cookie in tmpCookies)
            {
                cookie.Domain = URI.Host;
                if (cookie.Value.Length != 0)
                {
                    request.CookieContainer.Add(cookie);
                }
                else
                {
                    //Console.WriteLine(cookie.ToString());
                }
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(@"AcctName=anhuiqb01");
            sb.Append("&Passwd=" + pass);
            sb.Append("&ExtraCode=" + yanzheng + "");
            sb.Append("&x=26");
            sb.Append("&y=13");
            string para = sb.ToString();

            StringBuilder UrlEncoded = new StringBuilder();
            Char[] reserved = { '?', '=', '&' };
            byte[] SomeBytes = null;
            if (para != null)
            {
                int i = 0, j;
                while (i < para.Length)
                {
                    j = para.IndexOfAny(reserved, i);
                    if (j == -1)
                    {
                        UrlEncoded.Append(HttpUtility.UrlEncode(para.Substring(i, para.Length - i), System.Text.Encoding.Default));
                        break;
                    }
                    UrlEncoded.Append(HttpUtility.UrlEncode(para.Substring(i, j - i), System.Text.Encoding.Default));
                    UrlEncoded.Append(para.Substring(j, 1));
                    i = j + 1;
                }
                SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString());
                request.ContentLength = SomeBytes.Length;
                Stream newStream = request.GetRequestStream();
                newStream.Write(SomeBytes, 0, SomeBytes.Length);
                newStream.Close();
            }
            string resultstring_Post = "";

            //request.RequestUri = host;
            //request.CookieContainer = new CookieContainer();

            HttpWebResponse res = (HttpWebResponse)request.GetResponse();
            Stream ReceiveStream = res.GetResponseStream();

            StreamReader sr = new StreamReader(ReceiveStream, Encoding.Default);
            string temp = sr.ReadToEnd();

            System.Text.Encoding gb = System.Text.Encoding.GetEncoding("utf-8");
            byte[] b = gb.GetBytes(temp);
            string strResult = gb.GetString(b);

            this.richTextBox1.Text = temp;

            Regex r = new Regex(@"PcacheTime=\d*");
            Match m = r.Match(temp);
            time = m.Value;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            GetFanhui();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //得到成功返回
            string host2 = "http://qb.qq.com/cgi-bin/qbas_tran_sell_display.cgi";
            Uri URI = new Uri(host2);
            HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(host2);
            request2.Method = "post";

            request2.ServicePoint.Expect100Continue = false;
            request2.Timeout = 30000;
            request2.ContentType = "application/x-www-form-urlencoded";
            request2.Headers.Add("UA-CPU", "x86");
            request2.Headers.Add("Accept-Encoding", "gzip, deflate");
            request2.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;  Embedded Web Browser from: http://bsalsa.com/; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 1.1.4322; .NET CLR 2.0.50727; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            request2.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            request2.KeepAlive = true;

            request2.CookieContainer = new CookieContainer();

            foreach (System.Net.Cookie cookie in tmpCookies)
            {
                cookie.Domain = URI.Host;
                if (cookie.Value.Length != 0)
                {
                    request2.CookieContainer.Add(cookie);
                }
                else
                {
                    //Console.WriteLine(cookie.ToString());
                }
            }

            HttpWebResponse myHttpWebResponse = (HttpWebResponse)request2.GetResponse();
            Stream receiveStream = myHttpWebResponse.GetResponseStream();
            Encoding encode = System.Text.Encoding.UTF8;
            //StreamReader readStream = new StreamReader(receiveStream, encode);
            StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.GetEncoding("gb2312"));

            string temp = readStream.ReadToEnd();
            this.richTextBox1.Text = temp;
        }

        private void Form4_Load(object sender, EventArgs e)
        {

        }
    }
}